package com.ktz.sharedpreferences;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends Activity {
	private SharedPreferences prefs;
	private String namePref = "MyPref";
	private EditText editText;
	private SeekBar seekBar;
	private Button btnSave;
	
	private static final String FONT_SIZE_KEY = "fontsize";
	private static final String TEXT_VALUE_KEY = "textvalue";
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		editText = (EditText) findViewById(R.id.editText1);
		seekBar = (SeekBar) findViewById(R.id.seekBar1);
		btnSave = (Button) findViewById(R.id.button1);
		
		btnSave.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				prefs = getSharedPreferences("namePref", MODE_PRIVATE);
				SharedPreferences.Editor editor = prefs.edit();
				editor.putFloat(FONT_SIZE_KEY, editText.getTextSize());
				editor.putString(TEXT_VALUE_KEY, editText.getText().toString());
				editor.commit();
				Toast.makeText(getBaseContext(), "Successfull v2", Toast.LENGTH_SHORT).show();
				
			}
		});
		
		prefs = getSharedPreferences("namePref", MODE_PRIVATE);
		
		float fontsize = prefs.getFloat(FONT_SIZE_KEY, 12);
		seekBar.setProgress((int) fontsize);
		
		editText.setText(prefs.getString(TEXT_VALUE_KEY, ""));
		editText.setTextSize(seekBar.getProgress());
		
		seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				editText.setTextSize(progress);
				
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
